﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using ComicIsland.App.Resources;
using ComicIsland.Common;
using System.Reflection;
using ComicIsland.Common.WebHelper;

namespace ComicIsland.App
{
    public partial class MainPage : PhoneApplicationPage
    {
        // 构造函数
        public MainPage()
        {
            InitializeComponent();

            // 用于本地化 ApplicationBar 的示例代码
            //BuildLocalizedApplicationBar();



            test();

        }

        void test()
        {
            string requestUrl = "http://58.215.139.194/comic/homelist";
            int e = DateTime.Now.Subtract(DateTime.Parse("1979-01-01 00:00:00")).Seconds;

            List<QueryKeyValue> queries = new List<QueryKeyValue>();
            queries.Add(new QueryKeyValue() { Key = "bookid", Value = "2" });
            queries.Add(new QueryKeyValue() { Key = "partid", Value = "40331" });
            queries.Add(new QueryKeyValue() { Key = "e", Value = e.ToString() });

            string a = "bookid=2&partid=40331&e=1376364003";
            LogHelper.LogInfo(new HMACSHAHelper().SignHMACSHA1(a));

            WebTaskHelper webTask = new WebTaskHelper(new Uri(requestUrl));
            IAsyncResult ia = webTask.BeginWebTask(delegate(IAsyncResult callBackResult)
            {
                try
                {
                    this.Dispatcher.BeginInvoke(() =>
                    {
                        #region 获取结果处理
                        string result = webTask.EndWebTask(callBackResult);

                        if (string.IsNullOrWhiteSpace(result))
                        {
                            LogHelper.LogError("Result is empty.");
                            //MessageToastManager.Show("未获取到数据！", MessageToastLevel.Error);
                            return;
                        }
                        //
                        if (result.StartsWith("exception:"))
                        {
                            LogHelper.LogError(result);
                            //MessageToastManager.Show("网络连接失败！", MessageToastLevel.Error);
                            return;
                        }
                        LogHelper.LogInfo(result, this.GetType(), MethodBase.GetCurrentMethod());
                        //



                        #endregion 获取结果处理
                    });
                }
                catch (Exception err)
                {

                }
            }, webTask);
        }
    }

    public class QueryKeyValue
    {
        public string Key { get; set; }
        public string Value { get; set; }
    }
}